/*
 * @Author: Qzzz 2832160409@qq.com
 * @Date: 2024-09-29 19:17:58
 * @LastEditors: Qzzz 2832160409@qq.com
 * @LastEditTime: 2024-10-06 00:12:34
 * @FilePath: \bishe\后端\routes\article.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 用户路由单元
var express = require('express');
var router = express.Router();
// 导入连接池对象
const sql = require('../db/index')
// 引入jwt
const jwt = require('jsonwebtoken')

// 引入响应体
const Response = require('../utils/response')

// 新增文章接口
router.post('/addArticle', async (req, res) => {
  const { title, content, categoryId, publishTime, author } = req.body;

  try {
    // 插入文章到category_article表
    const query = `INSERT INTO category_article (title, content, category_id, publish_time, author) VALUES (?, ?, ?, ?, ?)`; // 添加了缺少的逗号
    await sql.query(query, [title, content, categoryId, publishTime, author]);
    // 如果数据库操作成功
    res.send(new Response(200,'新增成功',null))
  } catch (err) {
    console.error('数据库错误:', err); // 在服务器端记录错误详情
    res.send(new Response(500,'数据库错误',null))

  }
});

  // 根据ID删除文章接口
router.get('/deleteArticle', async (req, res) => {
  const { id } = req.query; // 从URL参数中获取文章ID

  try {
    // 删除category_article表中指定ID的文章
    const query = `DELETE FROM category_article WHERE id = ?`;
    await sql.query(query, [id]);
    // 如果数据库操作成功
    res.send(new Response(200, '删除成功', null));
  } catch (err) {
    res.send(new Response(500, '数据库错误', err));
  }
});


module.exports = router;
